package com.yuyou.train.admin.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuyou.train.admin.mapper.CommSmsVerCodeMapper;
import com.yuyou.train.admin.service.ICommSmsVerCodeService;
import com.yuyou.train.admin.service.ISmsService;
import com.yuyou.train.service.api.admin.entity.CommSmsVerCode;
import com.yuyou.train.util.bean.ResBoolSimpleInfo;
import com.yuyou.train.util.constant.CommCons;
import com.yuyou.train.util.enums.admin.CommSmsVerCodeEnums;
import com.yuyou.train.util.message.admin.CommSmsVerCodeMess;
import com.yuyou.train.util.tool.DateUtil;
import com.yuyou.train.util.tool.GenerateVerCodeUtils;
import com.yuyou.train.util.tool.Sequence;
import com.yuyou.train.util.tool.UUIDUtil;

@Service
public class CommSmsVerCodeServiceImpl implements ICommSmsVerCodeService {

	@Autowired
	private CommSmsVerCodeMapper commSmsVerCodeMapper;

	@Autowired
	private ISmsService smsService;
	

	@Override
	public int deleteByPrimaryKey(String id) {
		return commSmsVerCodeMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(CommSmsVerCode commSmsVerCode) {
		commSmsVerCode.setId(Sequence.getSequence().getNextId());
		commSmsVerCode.setCreateTime(new Date());
		return commSmsVerCodeMapper.insert(commSmsVerCode);
	}

	@Override
	public CommSmsVerCode selectByPrimaryKey(String id) {
		return commSmsVerCodeMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(CommSmsVerCode commSmsVerCode) {
		commSmsVerCode.setUpdateTime(new Date());
		return commSmsVerCodeMapper.updateByPrimaryKeySelective(commSmsVerCode);
	}

	@Override
	public CommSmsVerCode selectLastByMobile(String mobile) {
		return commSmsVerCodeMapper.selectLastByMobile(mobile);
	}


	@Override
	public CommSmsVerCode selectLastByMobileAndType(String mobile, String type) {
		return commSmsVerCodeMapper.selectLastByMobileAndType(mobile, type);
	}

	@Override
	public CommSmsVerCode selectLastByMobileAndTypeAndCode(String mobile, String type, String code) {
		return commSmsVerCodeMapper.selectLastByMobileAndTypeAndCode(mobile, type, code);
	}


	@Override
	public ResBoolSimpleInfo<String> addSmsVerCode(String mobile, String type) {

		CommSmsVerCode commSmsVerCode=new CommSmsVerCode();
		String code=GenerateVerCodeUtils.generateVerCode(6);
		
		commSmsVerCode.setId(Sequence.getSequence().getNextId());
		commSmsVerCode.setCode(code);
		commSmsVerCode.setMobile(mobile);
		commSmsVerCode.setContent(String.format(CommSmsVerCodeMess.SMS_CONTENT, code));
		commSmsVerCode.setType(type);
		commSmsVerCode.setVerTime(0);
		commSmsVerCode.setStatus( CommSmsVerCodeEnums.status.init.getValue());
		this.insert(commSmsVerCode);
		//调用短信服务
		int smsResult = smsService.sendCode(mobile, type, code);		
		CommSmsVerCode up=new CommSmsVerCode();
		if(0 == smsResult){		
			up.setStatus( CommSmsVerCodeEnums.status.succ.getValue());
			this.updateByPrimaryKeySelective(up);
			return ResBoolSimpleInfo.getSuccessInfo();
		}else{
			up.setStatus( CommSmsVerCodeEnums.status.fail.getValue());
			this.updateByPrimaryKeySelective(up);
			return new ResBoolSimpleInfo<String>(false, CommSmsVerCodeMess.SMS_SEND_FAILED);
		}
	}

	@Override
	public ResBoolSimpleInfo<String> verSmS(String mobile,String verCode) {
		ResBoolSimpleInfo<String> res=new ResBoolSimpleInfo<String>();
		CommSmsVerCode commSmsVerCode=this.selectLastByMobile(mobile);
		if(commSmsVerCode==null){
			//验证码不存在
			res.setMessage(CommSmsVerCodeMess.VER_CODE_NOT_EXIST);
			res.setSuccess(false);
			return res;
		}
		if(!verCode.equals(commSmsVerCode.getCode())){
			//验证码不正确
			res.setMessage(CommSmsVerCodeMess.VER_CODE_MATCH);
			res.setSuccess(false);
			return res;
		}
		if(commSmsVerCode.getVerTime()>CommCons.COMM_SMS_VER_CIDE_VER_TIME){
			//校验次数已满
			res.setMessage(CommSmsVerCodeMess.VER_CODE_TIME_LIMIT);
			res.setSuccess(false);
			return res;
		}
		if(DateUtil.getTimeDelta(commSmsVerCode.getCreateTime(), new Date())> CommCons.COMM_SMS_VER_CIDE_VER_SECOND){
			//时间已失效
			res.setMessage(CommSmsVerCodeMess.VER_CODE_SECOND_EXPIRE);
			res.setSuccess(false);
			return res;
		}			
		CommSmsVerCode update=new CommSmsVerCode();
		update.setId(commSmsVerCode.getId());
		update.setVerTime(commSmsVerCode.getVerTime()+1);
		this.updateByPrimaryKeySelective(update);
		return ResBoolSimpleInfo.getSuccessInfo();
	}



	
	

}